import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtQuick.Shapes 1.15
MainView {
    id: root
    width: 800
    height: 391
    property color btnColor: "#7080C0"
    property int btnHeight: 50
    property int btnWidth: 128
    signal startLoad
    Component.onCompleted: {
        startLoad()
    }
    onStartLoad: {
        holdParaLoader.visible=true
        holdTimmer.start()
        mymodbus.getHoldMode()
        mymodbus.getHoldTriggerCondition()
        mymodbus.getZeroValue()
        mymodbus.getHoldResult()
    }
    Rectangle{
        id:holdMainRec
        anchors.fill:parent
        color:"transparent"
    GridLayout{
        id: generalSettingsLayout
        rows: 2
        columns: 4
        anchors.centerIn: parent
        rowSpacing: 40
        Button
        {
            Layout.row:0
            Layout.column:0
            Layout.preferredWidth: btnWidth
            Layout.preferredHeight: btnHeight
            // x: 152
            // y: 13
            text: qsTr("保持模式")
            font.pixelSize: 18
            background: Rectangle{
                color: btnColor
                    }
            onClicked: {
                cmbHoldMode.popup.open()
                //miniKeyBoard.lineValue = "";
                //miniKeyBoard.textInput=oldPwd;
            }
        }
        MyComboBox
        {
            id:cmbHoldMode
            Layout.row:0
            Layout.column:1
            Layout.preferredWidth: btnWidth
            Layout.preferredHeight: btnHeight
            borderColor:"white"
            fillColor:"#0A3653"

            model:[qsTr("无保持功能"),qsTr("峰值保持"),qsTr("谷值保持"),qsTr("峰-谷值保持"),qsTr("采样值保持")]
            currentIndex:mymodbus.holdMode

        }
//        Rectangle {
//            id: recHoldMode
//            width:300
//            height:30
//            // x: 218
//            // y: 7
//            color: "transparent"
//            border.width: 0
//            border.color: "white"
//            Layout.row:0
//            Layout.column:1
//            Layout.preferredWidth: btnWidth
//            Layout.preferredHeight: btnHeight
//            // 绘制一条边框
//            Shape {
//                anchors.fill: parent
//                ShapePath {
//                    strokeWidth: 3
//                    strokeColor: "white"
//                    strokeStyle: ShapePath.SolidLine
//                    startX: 0
//                    startY: recHoldMode.height
//                    PathLine {
//                        x: recHoldMode.width
//                        y: recHoldMode.height
//                    }
//                }
//            }
//            TextInput{
//                id:holdMode
//                anchors.fill: parent
//                echoMode: TextInput.Password
//                color:"white"
//                font.pixelSize: 25
//                horizontalAlignment: Text.AlignHCenter
//                MouseArea {
//                       anchors.fill: parent
//                       onClicked: {
//                           //miniKeyBoard.lineValue = "";
//                           //miniKeyBoard.textInput=oldPwd;
//                       }
//                   }

//            }
//        }
            Button
            {
                Layout.row:0
                Layout.column:2
                Layout.preferredWidth: btnWidth
                Layout.preferredHeight: btnHeight
                // x: 152
                // y: 13
                text: qsTr("触发条件")
                font.pixelSize: 18
                background: Rectangle{
                    color: btnColor
                        }
                onClicked: {
                    cmbHoldTriggerCondition.popup.open()
                    //miniKeyBoard.lineValue = "";
                    //miniKeyBoard.textInput=oldPwd;
                }
            }
            MyComboBox
            {
                id:cmbHoldTriggerCondition
                Layout.row:0
                Layout.column:3
                Layout.preferredWidth: btnWidth
                Layout.preferredHeight: btnHeight
                borderColor:"white"
                fillColor:"#0A3653"

                model:[qsTr("外部触发"),qsTr("零区触发")]
                currentIndex:mymodbus.holdTriggerCondition
            }
//            Rectangle {
//                id: recTriggerCondition
//                width:300
//                height:30
//                // x: 218
//                // y: 7
//                color: "transparent"
//                border.width: 0
//                border.color: "white"
//                Layout.row:0
//                Layout.column:3
//                Layout.preferredWidth: btnWidth
//                Layout.preferredHeight: btnHeight
//                // 绘制一条边框
//                Shape {
//                    anchors.fill: parent
//                    ShapePath {
//                        strokeWidth: 3
//                        strokeColor: "white"
//                        strokeStyle: ShapePath.SolidLine
//                        startX: 0
//                        startY: recTriggerCondition.height
//                        PathLine {
//                            x: recTriggerCondition.width
//                            y: recTriggerCondition.height
//                        }
//                    }
//                }
//                TextInput{
//                    id:inGraduation
//                    anchors.fill: parent
//                    echoMode: TextInput.Password
//                    color:"white"
//                    font.pixelSize: 25
//                    horizontalAlignment: Text.AlignHCenter
//                    MouseArea {
//                           anchors.fill: parent
//                           onClicked: {
//                               // miniKeyBoard.lineValue = "";
//                               // miniKeyBoard.textInput=oldPwd;
//                           }
//                       }

//                }
//            }
    Button
    {
        Layout.row:1
        Layout.column:0
        Layout.preferredWidth: btnWidth
        Layout.preferredHeight: btnHeight
        // x: 152
        // y: 13
        text: qsTr("触发零区值")
        font.pixelSize: 18
        background: Rectangle{
            color: btnColor
                }
        onClicked: {
            //miniKeyBoard.lineValue = "";
            //miniKeyBoard.textInput=oldPwd;
            //rectangle.selectedView=16
            enterNumberPanel.lastButtonIndex = 4;
            enterNumberPanel.lastViewIndex =8;
            enterNumberPanel.minimum=0;
            enterNumberPanel.clearText()
            enterNumberPanel.maximum=999999
            enterNumberPanel.itemName=qsTr("触发零区值")
            enterNumberPanel.itemValue=zeroValue.text
            enterNumberPanel.textInput=zeroValue;
            enterNumberPanel.visible=true;
            enterNumberPanel.lastRec=holdMainRec
            holdMainRec.visible=false
        }
    }

    Rectangle {
        id: recZoroValue
        width:300
        height:30
        // x: 218
        // y: 7
        color: "transparent"
        border.width: 0
        border.color: "white"
        Layout.row:1
        Layout.column:1
        Layout.preferredWidth: btnWidth
        Layout.preferredHeight: btnHeight
        // 绘制一条边框
        Shape {
            anchors.fill: parent
            ShapePath {
                strokeWidth: 3
                strokeColor: "white"
                strokeStyle: ShapePath.SolidLine
                startX: 0
                startY: recZoroValue.height
                PathLine {
                    x: recZoroValue.width
                    y: recZoroValue.height
                }
            }
        }
        TextInput{
            id:zeroValue
            anchors.fill: parent
            readOnly: true
            //echoMode: TextInput.Password
            color:"white"
            font.pixelSize: 25
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignBottom
            text:mymodbus.zeroValue
            MouseArea {
                   anchors.fill: parent
                   onClicked: {
                       //miniKeyBoard.lineValue = "";
                       //miniKeyBoard.textInput=oldPwd;
                       //rectangle.selectedView=16
                       enterNumberPanel.lastButtonIndex = 4;
                       enterNumberPanel.lastViewIndex =9;
                       enterNumberPanel.minimum=0;
                       enterNumberPanel.clearText()
                       enterNumberPanel.maximum=999999
                       enterNumberPanel.itemName=qsTr("触发零区值")
                       enterNumberPanel.itemValue=zeroValue.text
                       enterNumberPanel.textInput=zeroValue;
                       enterNumberPanel.visible=true;
                       enterNumberPanel.lastRec=holdMainRec
                       holdMainRec.visible=false
                   }
               }

        }
    }
        Button
        {
            Layout.row:1
            Layout.column:2
            Layout.preferredWidth: btnWidth
            Layout.preferredHeight: btnHeight
            // x: 152
            // y: 13
            text: qsTr("保持结果")
            font.pixelSize: 18
            background: Rectangle{
                color: btnColor
                    }
            onClicked: {
                //miniKeyBoard.lineValue = "";
                //miniKeyBoard.textInput=oldPwd;
            }
        }

        Rectangle {
            id: recHoldResult
            width:300
            height:30
            // x: 218
            // y: 7
            color: "transparent"
            border.width: 0
            border.color: "white"
            Layout.row:1
            Layout.column:3
            Layout.preferredWidth: btnWidth
            Layout.preferredHeight: btnHeight
            // 绘制一条边框
            Shape {
                anchors.fill: parent
                ShapePath {
                    strokeWidth: 3
                    strokeColor: "white"
                    strokeStyle: ShapePath.SolidLine
                    startX: 0
                    startY: recHoldResult.height
                    PathLine {
                        x: recHoldResult.width
                        y: recHoldResult.height
                    }
                }
            }
            TextInput{
                id:holdResulot
                anchors.fill: parent
                readOnly: true
                //echoMode: TextInput.Password
                text:mymodbus.holdResult
                color:"white"
                font.pixelSize: 25
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignBottom
                MouseArea {
                       anchors.fill: parent
                       onClicked: {
                           // miniKeyBoard.lineValue = "";
                           // miniKeyBoard.textInput=oldPwd;
                       }
                   }

            }
        }
}

    Button
    {
        id:btnHoldSettingsOK
        x: 572
        y: 281
        //x: 675
        //y: 20
        width: 100
        height: 100
        background: Rectangle{
            border.width: 0
            color: "#C070B0"
                }

        text: qsTr("确定")
        font.pixelSize: 25
        palette.buttonText: "white"
        Connections {
            target: btnHoldSettingsOK
            onClicked: {
                holdParaLoader.visible =true
                var res =
                mymodbus.setHoldModeCondition(cmbHoldMode.currentIndex, cmbHoldTriggerCondition.currentIndex)
                && mymodbus.setZeroValue(Number(zeroValue.text))
                holdParaLoader.visible=false
                if(res){
                    holdSaveOKDialog.title =qsTr("保存成功！")
                    holdSaveOKDialog.open()
                    holdSaveOKDialog.accepted.connect(function(){
                        holdSaveOKDialog.close()

                        //rectangle.selectedView =0;
                        bottomBar.selected=0;
//                        myLoader.visible=false;
//                        myLoader.visible=true;
                        myLoader.source="StatusView.qml"
                        //statusView.visible=true;
                    })
                }
                else{
                    holdSaveOKDialog.title =qsTr("保存失败！")
                    holdSaveOKDialog.open()
                    holdSaveOKDialog.accepted.connect(function(){
                        holdSaveOKDialog.close()
                    })
                }
            }
        }
    }

    Button
    {
        id:btnReturn
        x: 688
        y: 281
        //x: 675
        //y: 20
        width: 100
        height: 100
        background: Rectangle{
            border.width: 0
            color: "#B0B0D0"
                }

        text: qsTr("返回")
        font.pixelSize: 25
        palette.buttonText: "white"
        Connections {
            target: btnReturn
            onClicked: {
                //rectangle.selectedView = 4
//                myLoader.visible=false;
//                myLoader.visible=true
                myLoader.source="SettingsPanel.qml"
            }

    }
    }
   }

    Timer{
       id:holdTimmer
       interval: 1200
       running: false
       repeat: false
       onTriggered: holdParaLoader.visible = false
   }

    EnterNumberPanel{
          id:enterNumberPanel
          width: 800
          height: 391
          visible:false
    }
   MyBuzyIndicator{
       anchors.centerIn: parent
       id: holdParaLoader
       visible:true
       z:101
   }
    Dialog {
        anchors.centerIn: parent
        id: holdSaveOKDialog
        //title: "提示"
        standardButtons: Dialog.Ok
        //title:qsTr("保存成功！")
        modal:true
        closePolicy: "CloseOnEscape"
        //onAccepted: holdSaveOKDialog.close()
        z:100
        //onRejected: console.log("Cancel clicked")
    }
}

